<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
use app\admin\controller\Base;
use app\admin\model\Cate as CateModel;
class Cate extends Base{
	public function index(){
		return $this->fetch();
	}

	public function lists(){
		$list=cateModel::paginate(15);
		$this->assign('list',$list);
		return $this->fetch('list');
	}



	public function add(){
		if (request()->isPost()) {
			$data=[
				'catename'=>input('catename'),
			];
			$validate= \think\Loader::validate('cate');
			// var_dump($validate->scene('add'));die;
			// var_dump($validate->scene('add')->check());die;
			if (!$validate->scene('add')->check($data)) {
				return $this->error($validate->getError());
			}
			if (Db::name('cate')->insert($data)) {
				return $this->success('添加栏目成功！','lists','','1');
			}else{
				return $this->error('添加栏目失败！');
			}
			return;
		}
		return $this->fetch('add');
	}

	public function edit(){
		$id=input('id');
		// echo "<meta charset='utf-8'>";
		$res=db('cate')->find($id);
		if (request()->isPost()) {
			$data=[
				'id'=>input('id'),
				'catename'=>input('catename'),
			];
			//数据的校验 
			// var_dump($data);die;
			$validate= \think\Loader::validate('cate');
			// var_dump($validate->scene('edit')->check($data));die;
			if (!$validate->scene('edit')->check($data)) {
				return $this->error($validate->getError());
			}

			$res=db('cate')->update($data);
			if ($res) {
				return $this->success('修改成功！','lists');
			}else{
				return $this->success('修改失败！');
			}
		}
		$this->assign('cate',$res);
		return $this->fetch('edits');
	}

	public function delete(){
		$id=input('id');
		$res=db('cate')->delete($id);
		if ($res) {
			$this->success("删除成功！",'lists');
		}else{
			$this->success("删除失败！",'');
		}
	}

}
?>